﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        
            public int PageSize = 4; // We will change this later
            private IProductRepository repository;
            public ProductController(IProductRepository repoParam)
            {
                repository = repoParam;
            }
            //public ViewResult List(int page = 1)
            //{
            //    return View(repository.Products
            //    .OrderBy(p => p.ProductID)
            //    .Skip((page - 1) * PageSize)
            //    .Take(PageSize));
            //}

            public ViewResult List(string category, int page = 1)
            {
                ProductsListViewModel viewModel = new ProductsListViewModel
                {
                    Products = repository.Products
                    
                    .Where(p => category == null || p.Category.Description == category)
                    .OrderBy(p => p.ProductID)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = PageSize,
                        TotalItems = category == null ?
                            repository.Products.Count() :
                            repository.Products.Where(e => e.Category.Description == category).Count()
                    },
                    CurrentCategory = category
                };
                return View(viewModel);
            }

            [HttpPost]
            public ActionResult Edit(Product product, HttpPostedFileBase image)
            {
                if (ModelState.IsValid)
                {
                    if (image != null)
                    {
                        product.ImageMimeType = image.ContentType;
                        product.ImageData = new byte[image.ContentLength];
                        image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                    }
                    // save the product
                    repository.SaveProduct(product);
                    // add a message to the viewbag
                    TempData["message"] = string.Format("{0} has been saved", product.Name);
                    // return the user to the list
                    return RedirectToAction("Index");
                }
                else
                {
                    // there is something wrong with the data values
                    return View(product);
                }
            }

            public FileContentResult GetImage(int productId)
            {
                Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
                if (prod != null)
                {
                    return File(prod.ImageData, prod.ImageMimeType);
                }
                else
                {
                    return null;
                }
            }

    }
}
